home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / thumbnail test / shrinktobw.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.1 KB  |  109 lines

  1. /*
  2.     File:        ShrinkToBW.c
  3.  
  4.     Contains:    this file contains a routine to illustrate the use of copybits to generate thumbnail images
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include <QDOffscreen.h>
  25. #include <PictUtils.h>
  26. #include <Windows.h>
  27.  
  28. extern RGBColor kRGBBlack ;
  29. extern RGBColor kRGBWhite ;
  30. extern Point gStaggerPos ;
  31.  
  32. void ShrinkToBWPict( WindowPtr theWindow );
  33.  
  34.  
  35. #define    SCALEFACTOR        15    // percent
  36.  
  37. void ShrinkToBWPict( WindowPtr theWindow )
  38. {
  39.     Rect        theBounds ;
  40.     GWorldPtr    theOldWorld, theNewWorld ;
  41.     OSErr        theErr ;
  42.     CGrafPtr    savedPort ;
  43.     WindowPtr    newWindow ;
  44.     GDHandle    oldDevice ;
  45.     
  46.     PaletteHandle    thePictPalette = nil ;
  47.     CTabHandle        thePictCTab = nil ;
  48.     
  49.     float        scaleFactor = SCALEFACTOR / 100.0 ;
  50.     
  51.     
  52.     // get the GWorld from the window refcon
  53.     theOldWorld = (GWorldPtr)GetWRefCon ( theWindow );
  54.     
  55.     // get the bounds of the window
  56.     theBounds = theOldWorld->portRect ;
  57.     
  58.     // apply the scaling factor
  59.     theBounds.top = scaleFactor * theBounds.top ;
  60.     theBounds.left = scaleFactor * theBounds.left ;
  61.     theBounds.bottom = scaleFactor * theBounds.bottom ;
  62.     theBounds.right = scaleFactor * theBounds.right ;
  63.     
  64.     // make a new one-bit gworld to hold the shrunken image
  65.     theErr = NewGWorld( &theNewWorld, 1, &theBounds, nil, nil, 0L ) ;
  66.     
  67.     if( theErr != noErr ) 
  68.         return ;
  69.     
  70.     // save the world
  71.     GetGWorld( &savedPort, &oldDevice ) ;
  72.     SetGWorld( theNewWorld, nil ) ;
  73.     
  74.     
  75.     RGBForeColor( &kRGBBlack ) ;        // ensure the fg and bg colors are 
  76.     RGBBackColor( &kRGBWhite ) ;        // as anticipated
  77.     EraseRect( &theBounds ) ;            // clear the area for the pict
  78.     PenMode( srcCopy ) ;                // ensure the t/f mode is as expected
  79.  
  80.  
  81.     // render the image into the offscreen buffer
  82.     CopyBits( &((GrafPtr)theOldWorld)->portBits,
  83.               &((GrafPtr)theNewWorld)->portBits,
  84.               &theOldWorld->portRect,
  85.               &theNewWorld->portRect,
  86.               patCopy | ditherCopy,
  87.               nil ) ;
  88.     
  89.     SetGWorld( savedPort, oldDevice ) ;
  90.     
  91.     // create the window
  92.     OffsetRect( &theBounds, gStaggerPos.h, gStaggerPos.v) ;
  93.     gStaggerPos.h += 16 ;
  94.     gStaggerPos.v += 16 ;        // heh - should roll these around, but you wont 
  95.                                 // create more than a couple of windows, will you  :-)
  96.                                  
  97.     newWindow  = NewCWindow( nil, &theBounds, "\pplayTime", true, 
  98.                                 documentProc, (WindowPtr)-1, true, (long)theNewWorld );    
  99.     
  100.     // make sure it is visible
  101.     ShowWindow( newWindow ) ;
  102.     
  103.     SetGWorld( (CGrafPtr)newWindow, nil ) ;
  104.     
  105.     // invalidate the content region of the window - we don't do any drawing to it here.
  106.     InvalRect ( &theBounds );
  107.     
  108.     SetGWorld( savedPort, oldDevice ) ;
  109. }